package com.yicheng.springcloud.controller;

import com.yicheng.springcloud.common.Result;
import com.yicheng.springcloud.entities.Payment;
import com.yicheng.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/payment/get/{id}")
    public Result getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);

        if(payment != null){
            return new Result(200,"查询成功,serverPort:"+serverPort,payment);
        }else{
            return new Result(444,"查询失败,id:"+id,null);
        }
    }

    @PostMapping(value = "/payment/create")
    public Result create(@RequestBody Payment payment){
        int num = paymentService.create(payment);
        if(num >0){
            return new Result(200,"保存成功,serverPort"+serverPort,num);
        }else {
            return new Result(444,"保存失败",null);
        }
    }

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB() {
        return serverPort;
    }
}
